<template>
	<div class="my-content-layers-group-list-item">
		<div
			class="root-item-content"
			:class="{ hover, select, 'list-mini': $store.chart.layerType === 'text' }"
			@click="clickHandle($event)"
			@mousedown="groupMousedownHandle($event)"
			@mouseenter="mouseenterHandle(componentGroupData)"
			@mouseleave="mouseleaveHandle(componentGroupData)"
			@contextmenu="handleContextMenu($event, componentGroupData, optionsHandle)"
		>
			<div class="flex-center item-content">
				<my-icon size="20" :icon="expend ? 'FolderOpen' : 'Folder'" />
				<el-text class="m-l-10 f1 w0 list-text" truncated>{{ componentGroupData.title }}</el-text>
				<layers-status :hover="hover" :status="status" />
			</div>
			<div :class="{ 'select-modal': select }"></div>
		</div>
		<n-collapse-transition :show="expend">
			<LayersListItem
				v-for="element in componentGroupData.groupList"
				:key="element.id"
				:componentData="element"
				:isGroup="true"
				@mousedown="mousedownHandle($event, element, componentGroupData.id)"
				@mouseenter="mouseenterHandle(element)"
				@mouseleave="mouseleaveHandle(element)"
				@contextmenu="handleContextMenu($event, componentGroupData, optionsHandle)"
			/>
		</n-collapse-transition>
	</div>
</template>

<script setup>
import LayersListItem from './LayersListItem.vue';
import LayersStatus from './LayersStatus.vue';
import { MenuEnum } from '@/enums/editPageEnum';
import { handleContextMenu } from '@/views/chart/hooks/useContextMenu.hook';

const props = defineProps({
	componentGroupData: {
		type: Object,
		required: true
	}
});

const expend = ref(false);

const select = computed(() => $store.chart.targetChart.selectId.find((e) => e === props.componentGroupData.id)),
	// 悬浮对象
	hover = computed(() => props.componentGroupData.id === $store.chart.targetChart.hoverId),
	// 组件状态 隐藏/锁定
	status = computed(() => props.componentGroupData.status);

// 右键
const optionsHandle = (targetList, allList, targetInstance) => {
	const filter = (menulist) => allList.filter((i) => menulist.includes(i.key));
	// 多选处理
	if ($store.chart.targetChart.selectId.length > 1) {
		return filter([MenuEnum.GROUP]);
	} else {
		const statusMenuEnums = [];
		if (targetInstance.status.lock) {
			statusMenuEnums.push(MenuEnum.LOCK);
		} else {
			statusMenuEnums.push(MenuEnum.UNLOCK);
		}
		if (targetInstance.status.hide) {
			statusMenuEnums.push(MenuEnum.HIDE);
		} else {
			statusMenuEnums.push(MenuEnum.SHOW);
		}
		return [
			...filter([MenuEnum.UN_GROUP]),
			{ divider: true },
			...targetList.filter((i) => !statusMenuEnums.includes(i.key))
		];
	}
};

// 点击
const clickHandle = (e) => {
	// 按下左键 + CTRL
	if ($store.chart.KeyboardActive?.ctrl) return;
	// 判断左右键
	expend.value = !expend.value;
	mousedownHandle(e, props.componentGroupData);
};

// 组点击事件
const groupMousedownHandle = (e) => {
	// 若此时按下了 CTRL, 表示多选
	const id = props.componentGroupData.id;
	if (e.buttons === 1 && $store.chart.KeyboardActive?.ctrl) {
		// 若已选中，则去除
		if ($store.chart.targetChart.selectId.includes(id)) {
			const exList = $store.chart.targetChart.selectId.filter((e) => e !== id);
			$store.chart.setTargetSelectChart(exList);
		} else {
			$store.chart.setTargetSelectChart(id, true);
		}
		return;
	}
	$store.chart.setTargetSelectChart(id);
};

// 公共点击事件
const mousedownHandle = (e, componentInstance, id) => {
	e.preventDefault();
	e.stopPropagation();
	$store.chart.setTargetSelectChart(id || componentInstance.id);
};

// 进入事件
const mouseenterHandle = (item) => {
	$store.chart.setTargetHoverChart(item.id);
};

// 移出事件
const mouseleaveHandle = () => {
	$store.chart.setTargetHoverChart(undefined);
};
</script>

<style lang="scss" scoped>
$centerHeight: 40px;
$centerMiniHeight: 28px;
$textSize: 10px;

@include b(content-layers-group-list-item) {
	position: relative;
	width: 90%;
	margin: 10px 5%;
	margin-bottom: 5px;
	transition: all 0.2s;
	:deep() {
		.my-content-layers-list-item {
			margin-right: 0 !important;
			width: 95% !important;
		}
	}

	&:hover {
		:deep() {
			.icon-item {
				opacity: 1;
			}
		}
	}

	.root-item-content {
		height: $centerHeight;
		cursor: pointer;
		border-radius: 5px;
		border: 1px solid rgba(0, 0, 0, 0);
		position: relative;
		&.hover,
		&:hover {
			background-color: var(--color-bg-4);
		}
		/* 选中 */
		&.select {
			border: 1px solid var(--el-color-primary);
			/* 需要设置最高级，覆盖 hover 的颜色 */
			background-color: rgba(0, 0, 0, 0);
			.list-img {
				border: 1px solid var(--el-color-primary) !important;
			}
		}

		// mini样式
		&.list-mini {
			height: $centerMiniHeight;
		}
	}
	.select-modal,
	.item-content {
		position: absolute;
		top: 0;
		left: 0;
	}
	.item-content {
		z-index: 1;
		padding: 6px 5px;
		justify-content: start !important;
		width: 100%;
		height: 100%;
	}
	.select-modal {
		width: 100%;
		height: 100%;
		opacity: 0.3;
		background-color: v-bind('themeColor');
	}
	.list-text {
		padding-left: 6px;
		font-size: $textSize;
	}

	.list-status-icon {
		margin-left: 3px;
	}
}
</style>
